package com.yichi.service;

import android.util.Log;

import com.jeremyliao.liveeventbus.LiveEventBus;
import com.yichi.global.EventDef;
import com.yichi.manager.IMqttMessageCallback;
import com.yichi.util.JsonUtil;
import com.yichi.util.MapUtil;
import com.yichi.util.StringUtil;

import java.util.Map;

/**
 * mqtt消息处理器
 */
public class MqttHandle implements IMqttMessageCallback {

    private final static String TAG = "MqttHandle";

    @Override
    public void handleMessage(String message) {
        Log.i(TAG, "mqtt接收到消息：" + message);

        //校验json格式
        //{"request":"opsdev","opsUserId":233,"userName":"游客","cabinetNum":"YCHDJSNJ000001","passWord":"8219"}
        //{"request":"take","cabinetID":5,"orderId":294}
        Map<String, Object> map;
        try {
            map = JsonUtil.parseMap(message);
        } catch (Exception e) {
            Log.e(TAG, e.getMessage(), e);
            return;
        }
        String request = MapUtil.getString(map, "request", "");
        if (StringUtil.isEmpty(request)) return;

        //分发事件
        switch (request) {
            case "take":
                LiveEventBus.get(EventDef.EVENT_WECHAT_TAKE_REQ, Map.class).post(map);
                break;
            case "return":
                LiveEventBus.get(EventDef.EVENT_WECHAT_RETURN_REQ, Map.class).post(map);
                break;
            case "opsdev":
                LiveEventBus.get(EventDef.EVENT_WECHAT_OPSDEV_REQ, Map.class).post(map);
                break;
        }
    }

}
